#!/usr/bin/env python
#coding:utf-8

import re

class BaseField(object):
    pass

class Field(BaseField):
    
    def __init__(self,default=None,display=None,required=True):
        self.__value = default
        self.__required = required
        self.Display = display
        
    @property
    def Value(self):
        return self.__value
    
    @Value.setter
    def Value(self,value):
        self.__value = value
        
    def __str__(self, *args, **kwargs):
        return self.__value
        
class StringField(Field):
    
    regular = re.compile('^\w+$')
    
    @property
    def Value(self):
        return self.__value
    
    @Value.setter
    def Value(self,value):
        if self.__validate(value):
            self.__value = value
        else:
            raise Exception('字段值不合法.')
        
    def __validate(self,value):
        if not value:
            if self.__required:
                return False
        else:
            result = StringField.regular.match(value)
            if not result:
                return False
        return True
            
            
            
            
            
            
        